﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SimpleWebApi.Business.Service.Interface;
using SimpleWebApi.Business.Service.Interface.DTO;
using SimpleWebApi.MyMigration.Models;
using System.Linq.Expressions;

namespace SimpleWebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ApiController : ControllerBase
    {
        private readonly ILogger<ApiController> _logger;

        private ICommodityService _comService;
        private ICompanyInfoService _companyService;

        private IMapper _mapper;

        public ApiController(ILogger<ApiController> logger, ICommodityService comService, ICompanyInfoService companyService, IMapper mapper)
        {
            _logger = logger;
            _comService = comService;
            _companyService = companyService;
            _mapper = mapper;
        }

        [HttpGet]
        public IEnumerable<CommodityDTO> GetCommodity(int Id)
        {
            Expression<Func<Commodity, bool>> funcWhere = null;
            funcWhere = a => a.Id == Id;
            var commodityList = _comService.Query(funcWhere);
            List<CommodityDTO> list = new List<CommodityDTO>();
            _mapper.Map<IQueryable<Commodity>, List<CommodityDTO>>(commodityList, list);

            return list;

        }

        [HttpGet]
        public CompanyInfoDTO GetCompanyInfo(int companyId)
        {
            var company = _companyService.GetCompany(companyId);
            CompanyInfoDTO dto = new CompanyInfoDTO();
            _mapper.Map<CompanyInfo, CompanyInfoDTO>(company, dto);

            return dto;

        }

        [HttpPost]
        public bool AddCommodity(CommodityCreateDTO commodity)
        {
            Commodity com = new Commodity();
            _mapper.Map<CommodityCreateDTO, Commodity>(commodity, com);
            return _comService.AddCommodity(com);
        }
    }
}